﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DatabaseHelper;
using System.IO;
using SDK10kWrapper;
using System.Configuration;
using RS232;
using UI.Online.SubForms;

namespace UI.Online
{
    public partial class frmOnlineUI2 : Form
    {
        public frmOnlineUI2()
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(this.frmOnlineUI_FormClosing);
        }

        public void frmOnlineUI_FormClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show(
                "Do you want to stop record and save event list and settings?",
                "Exit Application",
                 MessageBoxButtons.YesNo,
                 MessageBoxIcon.Question,
                 MessageBoxDefaultButton.Button2))
            {
                if (bIsRecording)
                {
                    bIsRecording = StopRecord();
                }
                
                SaveSettings();
                tlvProjectEvent.UpdateDB();
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void frmOnlineUI2_Load(object sender, EventArgs e)
        {
            LoadUI();
        }

        private void mnuFileProjects_Click(object sender, EventArgs e)
        {
            frmManageProjects frm = new frmManageProjects();
            frm.ShowDialog(); frm.Dispose();
            tlvProjectEvent.RefreshTLV();
            LoadProject();
        }

        private void mnuFileExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnStartRecord_Click(object sender, EventArgs e)
        {
            if ((cbbProject.SelectedIndex > -1) && (cbbPipeline.SelectedIndex > -1))
            {
                try
                {
                    if (!bIsRecording)
                    {
                        bIsRecording = StartRecord();
                        DisableRecordButtons();
                        DisableMenuItems();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            else
            {
                MessageBox.Show(
                    "Please select project and pipeline before record a video",
                    "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation,
                    MessageBoxDefaultButton.Button1);
            }
        }

        private void btnStopRecord_Click(object sender, EventArgs e)
        {
            try
            {
                if (bIsRecording)
                {
                    bIsRecording = StopRecord();
                    EnableRecordButtons();
                    EnableMenuItems();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void StartAutoTimer()
        {
            tmrAutoStop.Interval = 1000;
            tmrAutoStop.Elapsed += new System.Timers.ElapsedEventHandler(tmrAutoStop_Elapsed);
            tmrAutoStop.SynchronizingObject = this;
            tmrAutoStop.Start();
        }

        private void mnuFileComport_Click(object sender, EventArgs e)
        {
            if (!bIsRecording)
            {
                frmRS232Settings frm = new frmRS232Settings(
                    this, 
                    SurveyDataReader.Port.PortName,
                    SurveyDataReader.Port.BaudRate,
                    StringFormatSelection);
                frm.ShowDialog(); frm.Dispose();
            }
            else
            {
                MessageBox.Show(
                    "Please stop record to set up COM port.",
                    "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation,
                    MessageBoxDefaultButton.Button1);
            }
        }

        private void btnRecordFolder_Click(object sender, EventArgs e)
        {
            try
            {
                string recordFolder = currProjectLocation + "\\" + currPipelineName;
                System.Diagnostics.Process.Start("explorer.exe", recordFolder);
            }
            catch (Exception ex)
            {
            }
        }

        private void SetEventContent(string description, string generalComment, string comment)
        {
            txtDescription.Text = description;
            txtGeneralComment.Text = generalComment;
            txtComment.Text = comment;
        }

        private void SetEventButtonContent(string eventDescription)
        {
            switch (eventDescription)
            {
                case "Anode":
                    SetEventContent(eventDescription, "", "");
                    break;
                default:
                    SetEventContent(eventDescription, "", "");
                    break;
            }
        }

        private void EventButton_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Button btnEventButton = (System.Windows.Forms.Button)sender;
            SetEventButtonContent(btnEventButton.Text);
        }

        private void btnToExcel_Click(object sender, EventArgs e)
        {
            tlvProjectEvent.ExportToExcel();
        }

        private void mnuFileExport_Click(object sender, EventArgs e)
        {
            DBHelper dh = new DBHelper();
            dh.ExportAllEventsToExcel(3);
        }

        private void mnuFileExportHdd_Click(object sender, EventArgs e)
        {
            frmExportHdd frm = new frmExportHdd(this);
            frm.ShowDialog(); frm.Dispose();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            tlvProjectEvent.UpdateDB();
        }

        private void mnuSettingsCameras1_Click(object sender, EventArgs e)
        {
            if (mnuSettingsCameras1.Checked)
            {
                cam1.Stop();
                panel1.Visible = false;
                mnuSettingsCameras1.Checked = false;
                stsCamera1.Text = "Camera 1 is not connected.";
            }
            else
            {
                panel1.Visible = true;
                cam1.Connect();
                cam1.Play(false);
                mnuSettingsCameras1.Checked = true;
                stsCamera1.Text = "Camera 1 is connected.";
            }
        }

        private void mnuSettingsCameras2_Click(object sender, EventArgs e)
        {
            if (mnuSettingsCameras2.Checked)
            {
                cam2.Stop();
                panel2.Visible = false;
                mnuSettingsCameras2.Checked = false;
                stsCamera3.Text = "Camera 2 is not connected.";
            }
            else
            {
                panel2.Visible = true;
                cam2.Connect();
                cam2.Play(false);
                mnuSettingsCameras2.Checked = true;
                stsCamera2.Text = "Camera 2 is connected.";
            }
        }

        private void mnuSettingsCameras3_Click(object sender, EventArgs e)
        {
            if (mnuSettingsCameras3.Checked)
            {
                cam3.Stop();
                panel3.Visible = false;
                mnuSettingsCameras3.Checked = false;
                stsCamera3.Text = "Camera 3 is not connected.";
            }
            else
            {
                panel3.Visible = true;
                cam3.Connect();
                cam3.Play(false);
                mnuSettingsCameras3.Checked = true;
                stsCamera3.Text = "Camera 3 is connected.";
            }
        }

        private void mnuHelpAbout_Click(object sender, EventArgs e)
        {
            frmAbout frm = new frmAbout();
            frm.ShowDialog(); frm.Dispose();
        }

        private void CpStab_Click(object sender, EventArgs e)
        {
            txtDescription.Text = "CP Stab";
            txtComment.Text = txtCP.Text;
        }

        private void mnuHelpRegister_Click(object sender, EventArgs e)
        {
            frmRegister frm = new frmRegister();
            frm.ShowDialog(); frm.Dispose();
        }

    }
}
